package com.example.recall_application.mvp.ui.activity;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Binder;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;

import androidx.core.app.NotificationCompat;

import com.blankj.utilcode.util.SPUtils;
import com.example.recall_application.R;
import com.example.recall_application.app.Constants;
import com.example.recall_application.utils.FileUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MyService extends Service {

    private static final String TAG="MyService";
    private static final String ID="channel_1";
    private static final String NAME="前台服务";
    Handler handler = new Handler();//postDeley 实现循环定时
    private int reycleTime = 180*1000;//周期

    private int reycleTime1 = 1*1000;//周期

    private int deleyTime = 10*1000;//延迟多少时间后执行
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            FileUtils fileUtils = new FileUtils();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd/ HH:mm:ss");// HH:mm:ss 获取当前时间
            Date date = new Date(System.currentTimeMillis());
            fileUtils.writeData(simpleDateFormat.format(date),"pdf_log.txt");
            handler.postDelayed(runnable,reycleTime);
        }
    };//log任务


    Runnable runnable1 = new Runnable() {
        @Override
        public void run() {
            handler.postDelayed(runnable1,reycleTime1);
        }
    };//log任务


    private final IBinder iBinder = new LocalBinder();

    public class LocalBinder extends Binder{
        MyService getService(){
            return MyService.this;
        }
    }

    public MyService() {
    }



    public void setForeground(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager manager=(NotificationManager)getSystemService (NOTIFICATION_SERVICE);
            NotificationChannel channel=new NotificationChannel (ID,NAME, NotificationManager.IMPORTANCE_HIGH);
            manager.createNotificationChannel (channel);
            Notification notification=new Notification.Builder (this,ID)
                    .setContentTitle ("电话接听助手")
                    .setContentText ("电话接听助手正在值勤中...")
                    .setSmallIcon (R.drawable.ic_lanucher_two)
                    .setLargeIcon (BitmapFactory.decodeResource (getResources (), R.drawable.ic_lanucher_two))
                    .build ();
            startForeground (1,notification);
        }
        else{
            Notification notification1=new NotificationCompat.Builder(this)
                    .setContentTitle("标题")
                    .setContentText("内容")
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.drawable.ic_lanucher_two)
                    .build();
            startForeground(1,notification1);

        }
    }

    public void runLog(){
        handler.postDelayed(runnable,deleyTime);
        handler.postDelayed(runnable1,deleyTime);
    }

    
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        //throw new UnsupportedOperationException("Not yet implemented");
        return iBinder;
    }

}
